package com.br.aiops.api;

import com.br.aiops.listeners.ladder.LadderCycleStatus;
import com.br.aiops.listeners.ladder.LadderCycleStatusCache;
import com.br.aiops.schedule.AbstractSchedule;
import com.br.aiops.schedule.Schedules;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 列出所有阶梯调度
 */
@RestController
@RequestMapping("/api/schedule")
public class ScheduleController {

    @Autowired
    private Schedules schedules;

    /**
     * 返回schedule的统计状态，格式如下：
     * duration:
     *   current:
     *     counter_key: Status
     *   prev:
     *     counter_key: Status
     *
     * duration包括1、2、5
     * @return
     */
    @GetMapping
    public Map<String, Map<String, Map<String, AbstractSchedule.Status>>> getSchedule() {
        Map<String, Map<String, Map<String, AbstractSchedule.Status>>> result = Maps.newHashMap();
        List<Integer> durations = Lists.newArrayList(1, 2, 5);
        durations.forEach(duration -> {
            AbstractSchedule schedule = schedules.getSchedule(duration);
            Map<String, Map<String, AbstractSchedule.Status>> scheduleMap = Maps.newHashMap();
            scheduleMap.put("current", schedule.getCurrentCountStatus());
            scheduleMap.put("prev", schedule.getPrevCountStatus());

            result.put(duration.toString(), scheduleMap);
        });
        return result;
    }

    @GetMapping("/ladder")
    public Map<String, LadderCycleStatus> getLadder() {
        return LadderCycleStatusCache.getLadderCycleStatus();
    }
}
